import cv2 as cv
import requests
import os

def image_trans(src, root, size, debug=False):
    img = cv.imread(src)
    img = cv.resize(img, size)
    if debug:
        cv.imshow("img", img)
        cv.waitKey()
        cv.destroyAllWindows()
    cv.imwrite(root, img)
    print("Done!")
    return

def get_image_by_url(url, root, size, debug=False):
    request = requests.get(url)
    src = "temp.jpg"
    if request.ok:
        with open(src, "wb") as img:
            img.write(request.content)
            print(f"Download from {url}")
    else:
        print(request.status_code)
    image_trans(src, root, size, debug)
    os.remove(src)
    return

# urls = [
#     "http://p0.qhimg.com/bdm/0_0_100/t018ffab4c6eb805d71.jpg",
# ]
# for idx in range(1, len(urls) + 1, 1):
#     url  = urls[idx - 1]
#     root = R"C:\Users\21168\Desktop\HZ-WTGL-portal\static\pages\about\tech-services-img-1.jpg"
#     get_image_by_url(url, root, (500, 430), False)

src  = R"C:\Users\21168\Desktop\HZ-WTGL-portal\static\picture\2edd25e0049c2d547f3ce3fce5554ee.jpg"
root = R"C:\Users\21168\Desktop\HZ-WTGL-portal\static\pages\index\main-slider-1-1.jpg"
size = (1894, 1000)
image_trans(src, root, size)